package com.liangtian.mdm.dm.jsp.common;

import com.liangtian.mdm.dm.common.enums.Type;
import org.apache.commons.lang3.StringUtils;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.model.model.Model;

import java.util.List;
import java.util.Map;

public class DetailJspCreate {
    /**
     * * <p>Title: 动态拼接替换修改页面模板</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:23:20
     */
    @SuppressWarnings("unchecked")
    public static void createDetailPage(String formatJspStr, String readFilePath, Model model, String operType, Map<String, Object> result_map, List<AttrConfig> configLst) {
        try {
            String html = JspUtil.getTmpJspContent(readFilePath);  //根据路径获取自动生成的模板内容
            html = html.replace("@R#", "\r\n"); //将占位符替换成拼接的内容
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {
                html = html.replace("{mainContent}", "")
                        .replace("{mainEnd}", "")
                        .replace("{mainJsContent}", "")
                        .replace("{mainIdInputContent}", "<input type='hidden' id='mainId' value='<%= request.getParameter(\"mainId\")%>'>");
            } else {
                boolean bool = (boolean) result_map.get("flag");
                if (bool) {
                    List<Model> subModelLst = (List<Model>) result_map.get("subModel");
                    Map<String, String> resMap = JspUtil.spellMainContent(model, operType, subModelLst, "");
                    html = html.replace("{mainContent}", resMap.get("mainHead"))
                            .replace("{mainEnd}", resMap.get("mainEnd"))
                            .replace("{mainJsContent}", resMap.get("mainJs"))
                            .replace("{mainIdInputContent}", "<input type='hidden' ng-model='mId'  id='mId'>");
                } else {
                    html = html.replace("{mainContent}", "")
                            .replace("{mainEnd}", "")
                            .replace("{mainJsContent}", "")
                            .replace("{mainIdInputContent}", "");
                }
            }

            StringBuffer sb = new StringBuffer();
            for (AttrConfig attrConfig : configLst) {
                if (attrConfig.getAttrType() != null && "BLOB".equals(attrConfig.getAttrType())) {
//					sb.append("<div class='modal fade' id='"+attrConfig.getAttrColumn().toLowerCase()+"lookFilesModal'  tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true' data-backdrop='static'> \r\n")
//					  .append("  <div class='modal-dialog'> \r\n")
//					  .append("     <div class='modal-content'> \r\n")
//					  .append("     </div> \r\n")
//					  .append("  </div> \r\n")
//					  .append("</div> \r\n");
                    sb.delete(0, sb.length());
                    sb.append("<div class='modal fade' id='c_lookFilesModal'  tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true' data-backdrop='static'> \r\n")
                            .append("  <div class='modal-dialog'> \r\n")
                            .append("     <div class='modal-content'> \r\n")
                            .append("     </div> \r\n")
                            .append("  </div> \r\n")
                            .append("</div> \r\n");
                }
            }
            if (!StringUtils.isBlank(sb.toString())) {
                String str = "<form id='Fileform' name='Fileform'  method='post'> \r\n " +
                        "	<input type='hidden' id='dataId' name='dataId'>  \r\n " +
                        "	<input type='hidden' id='surveyType' name='surveyType'> \r\n " +
                        "	<input type='hidden' id='fileColumn' name='fileColumn'> \r\n" +
                        "</form> \r\n";
                html = html.replace("{fileForm}", str);
                html = html.replace("{fileModals}", sb.toString());
            } else {
                html = html.replace("{fileForm}", "");
                html = html.replace("{fileModals}", "");
            }

            String tableName = model.getTableName();
            html = html.replace("{detailHtml}", formatJspStr)  //格式化换行等占位符"{detailHtml}", formatJspStr
                    .replace("{formId}", tableName)
                    .replace("{formName}", tableName)
                    .replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js")
                    .replace("{title}", model.getModelName())
                    .replace("{ngApp}", tableName + operType + "App")
                    .replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN))
                    .replace("{DetailColumn}", JspUtil.spellColumn(model, Type.DETAIL))
                    .replace("{ngControl}", tableName + operType + "Control");

            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * * <p>Title: 获取页面根据type类型返回相应拼接字符串</p>
     *
     * @author : xiaoliu
     * @date :2016年7月26日下午1:28:49
     */
    //VARCHAR2-输入框、selectList-下拉选框、radioType-单选框、checkBoxType-复选框、popList-弹出列表、DATE-日期类型、BLOB-文件类型、
    //hideType-隐藏类型、mailType-邮件类型 CLOB-文本域  NUMBER-数字类型
    public static String searchReturnDetailPage(AttrConfig attrConfig, Model model, String opaType) {
        String colum = attrConfig.getAttrColumn().toLowerCase();
        StringBuilder sb = new StringBuilder();//将读取出来的数据，动态拼接成字符串
        String attrName = attrConfig.getAttrName();
        if (attrConfig.getAttrType() != null && "BLOB".equals(attrConfig.getAttrType())) {
            sb.append("     <div class='form-group draggable' id='" + colum + "Div'> \r\n")
                    .append("            <label class='col-sm-3 control-label'>" + attrName + ":</label> \r\n")
                    .append("            <label class='control-label' id='" + colum + "' name='" + colum + "' ng-bind='" + colum + "'> \r\n")
                    .append("            </label> \r\n")
                    .append("            <button type='button' id='" + colum + "lookButton' class='btn btn-outline btn-default' > \r\n")
                    .append("                  查看所有附件 \r\n")
                    .append("            </button> \r\n")
                    .append("            <button type='button' id='" + colum + "Button' class='btn btn-outline btn-default' ng-click='" + colum + "downLoad()'> \r\n ")
                    .append("                  附件下载  \r\n")
                    .append("            </button> \r\n")
//		        .append("            <div class='modal fade' id='"+colum+"lookFilesModal'  tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true' data-backdrop='static'> \r\n")
//		        .append("               <div class='modal-dialog'> \r\n")
//		        .append("                   <div class='modal-content'> \r\n")
//		        .append("                   </div> \r\n")
//		        .append("               </div> \r\n")
//		        .append("            </div> \r\n")
                    .append("       </div> \r\n");
        } else {
            sb.append("     <div class='form-group draggable' id='" + colum + "Div'> \r\n")
                    .append("            <label class='col-sm-3 control-label'>" + attrName + ":</label> \r\n")
                    .append("            <label class='control-label' id='" + colum + "' name='" + colum + "' ng-bind='" + colum + "'> \r\n")
                    .append("            </label> \r\n")
                    .append("       </div> \r\n");
        }

        return sb.toString();
    }

    public static void createCheckFilesPage(String readFilePath, Model model, String operType) {
        try {
            String html = JspUtil.getTmpJspContent(readFilePath);  //根据路径获取自动生成的模板内容
            html = html.replace("@R#", "\r\n"); //将占位符替换成拼接的内容
            String tableName = model.getTableName();
            html = html.replace("{tableName}", tableName)
                    .replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js");
            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
